
package com.chzc.fm.duotin.ui.mine;

import android.os.Environment;
import android.os.StatFs;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.MangerRecyclerAdapter;
import com.chzc.fm.duotin.bean.mine.SpecialBean;
import com.chzc.fm.duotin.db.AlbumDao;
import com.chzc.fm.duotin.db.ContentDao;
import com.chzc.fm.duotin.ui.other.BaseActivity;
import com.chzc.fm.duotin.utils.NumberUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

public class DownloadMangerActivity extends BaseActivity implements View.OnClickListener{

    private TextView textSpace, textAll, textDel;
    private RecyclerView recyclerView;

    private AlbumDao albumDao;
    private ContentDao contentDao;
    private List<SpecialBean> specialList;

    private MangerRecyclerAdapter adapter;

    private Vector<Boolean> checkVector;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_download_manger;
    }

    @Override
    protected void initViews() {
        textSpace = (TextView) findViewById(R.id.tv_manger_space);
        textAll = (TextView) findViewById(R.id.tv_manger_all);
        textDel = (TextView) findViewById(R.id.tv_manger_del);
        recyclerView = (RecyclerView) findViewById(R.id.rv_manger_content);
    }

    @Override
    protected void initEvents() {
        textAll.setOnClickListener(this);
        textDel.setOnClickListener(this);
    }

    @Override
    protected void init() {
        checkVector = new Vector<>();
        initDateBase();
    }

    private void initDateBase() {
        albumDao = new AlbumDao(getBaseContext());
        contentDao = new ContentDao(getBaseContext());
        specialList = new ArrayList<SpecialBean>();

        List<SpecialBean.AlbumBean> albumList = albumDao.getAlbums();

        long space = 0;
        for (SpecialBean.AlbumBean album : albumList) {
            SpecialBean specialBean = new SpecialBean();
            List<SpecialBean.ContentListBean> contentList;
            contentList = contentDao.getContent(album.getId());

            specialBean.setAlbum(album);
            specialBean.setContent_list(contentList);
            specialList.add(specialBean);

            for (SpecialBean.ContentListBean content:contentList) {
                space += Long.parseLong(content.getAudio_32_size());
            }
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(getBaseContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        adapter = new MangerRecyclerAdapter(getBaseContext(), specialList);
        recyclerView.setAdapter(adapter);

        adapter.setOnStateChangeListener(new MangerRecyclerAdapter.OnStateChangeListener() {
            @Override
            public void onStateChange(Boolean state) {
                if(state){
                    textAll.setText("全不选");
                }else{
                    textAll.setText("全选");
                }
            }
        });

        textSpace.setText("已下载" + NumberUtil.countSpace(space) + "        "
                + "剩余" + NumberUtil.countSpace(getAvailableInternalMemorySize()));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_manger_all:
                allCheck();
                break;
            case R.id.tv_manger_del:
                deleteDate();
                break;
        }
    }

    private void deleteDate() {

        checkVector = adapter.getVector();
        for(int i = 0; i<checkVector.size(); i++){
            if(checkVector.get(i)) {
                SpecialBean special = specialList.get(i);
                contentDao.deleteContentByAlbum(special.getAlbum().getId());
                albumDao.deleteAlbum(special.getAlbum().getId());
                specialList.remove(i);
                checkVector.remove(i);
                i--;
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void allCheck() {
        checkVector = adapter.getVector();
        boolean flag = true;
        for(int i = 0; i<checkVector.size(); i++){
            if(flag != checkVector.get(i)){
                flag = false;
            }
        }
        for(int i = 0; i<checkVector.size(); i++){
            checkVector.set(i, !flag);
        }
        adapter.setVector(checkVector);
        if(flag){
            textAll.setText("全选");
        }else{
            textAll.setText("全不选");
        }
    }
    /**
     * 获取手机内部空间大小
     * @return
     */
    static public long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();//Gets the Android data directory
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();      //每个block 占字节数
        long totalBlocks = stat.getBlockCount();   //block总数
        return totalBlocks * blockSize;
    }

    /**
     * 获取手机内部可用空间大小
     * @return
     */
    static public long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }
}
